<template>
    <view class="user-center">
        <!-- 用户信息头部 -->
        <view class="user-header" @tap="onPageTap('../user-info/userinfo')">
            <view class="header-content">
                <view class="avatar-wrapper">
                    <image class="avatar" :src="user.yonghuPhoto ? baseUrl+user.yonghuPhoto : '/static/center/face.jpeg'" />
                </view>
                <view class="user-info">
                    <text class="username">{{ user.yonghuName }}</text>
                </view>
                <view class="settings-btn">
                    <text class="cuIcon-settings"></text>
                </view>
            </view>
        </view>

        <!-- 功能菜单 -->
        <view class="menu-list">
            <!-- 修改密码 -->
            <view class="menu-item" @tap="onPageTap('../changepassword/changepassword')">
                <text class="icon cuIcon-moneybag"></text>
                <text class="title">修改密码</text>
                <text class="arrow cuIcon-right"></text>
            </view>

            <!-- 动态菜单项 -->
            <block v-for="item in menuList" :key="item.roleName">
                <block v-if="role == item.roleName">
                    <view v-for="(menu, index) in item.backMenu" :key="index">
                        <view v-for="(child, sort) in menu.child" :key="sort" 
                              class="menu-item" 
                              @tap="onPageTap(`../${child.tableName}/list?userId=${user.id}`)">
                            <text class="icon" :class="child.appFrontIcon"></text>
                            <text class="title">{{ child.menu }}</text>
                            <text class="arrow cuIcon-right"></text>
                        </view>
                    </view>
                </block>
            </block>
        </view>
    </view>
</template>

<script>
    import menu from '@/utils/menu'
    export default {
        data() {
            return {
                isH5Plus: true,
                user: {},
                tableName: '',
                role: '',
                menuList: [],
            };
        },
        computed: {
            baseUrl() {
                return this.$base.url;
            }
        },
        async onLoad() {
            this.role = uni.getStorageSync("role");
            let table = uni.getStorageSync("nowTable");
            let res = await this.$api.session(table);
            this.user = res.data;
            this.tableName = table;
            let menus = menu.list();
            this.menuList = menus;
        },
        async onShow() {
            this.role = uni.getStorageSync("role");
            let table = uni.getStorageSync("nowTable");
            let res = await this.$api.session(table);
            this.user = res.data;
            this.tableName = table;
            let menus = menu.list();
            this.menuList = menus;
        },
        methods: {
            chooseLocation() {
                uni.chooseLocation({
                    success: function (data) {
                    }
                })
            },
            onPageTap(url) {
                uni.navigateTo({
                    url: url,
                    fail: function() {
                        uni.switchTab({
                            url: url
                        });
                    }
                });
            }
        }
    }
</script>

<style lang="scss">
.user-center {
    background: #f5f7fa;
    min-height: 100vh;
}

/* 头部样式 */
.user-header {
    background: linear-gradient(135deg, #4a90e2, #6c5ce7);
    padding: 40rpx 30rpx 80rpx;
    position: relative;
    overflow: hidden;

    .header-content {
        display: flex;
        align-items: center;
        position: relative;
        z-index: 2;
    }

    .avatar-wrapper {
        width: 120rpx;
        height: 120rpx;
        border-radius: 50%;
        overflow: hidden;
        box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.1);
        background: #fff;
        
        .avatar {
            width: 100%;
            height: 100%;
        }
    }

    .user-info {
        flex: 1;
        margin-left: 30rpx;
        
        .username {
            color: #fff;
            font-size: 36rpx;
            font-weight: 600;
            line-height: 1.4;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
        }
    }

    .settings-btn {
        width: 80rpx;
        height: 80rpx;
        border-radius: 50%;
        background: rgba(255,255,255,0.2);
        display: flex;
        align-items: center;
        justify-content: center;
        
        .cuIcon-settings {
            color: #fff;
            font-size: 44rpx;
        }
    }
}

/* 菜单列表样式 */
.menu-list {
    margin: -40rpx 24rpx 0;
    position: relative;
    z-index: 2;
}

.menu-item {
    background: #fff;
    border-radius: 16rpx;
    padding: 0 30rpx;
    margin-bottom: 20rpx;
    display: flex;
    align-items: center;
    height: 100rpx;
    box-shadow: 0 6rpx 20rpx rgba(0,0,0,0.04);
    transition: all 0.2s;
    
    &:active {
        transform: scale(0.98);
        background: #f8f9fa;
    }

    .icon {
        width: 50rpx;
        font-size: 40rpx;
        color: #4a90e2;
        margin-right: 24rpx;
    }

    .title {
        flex: 1;
        font-size: 30rpx;
        color: #2d3436;
        font-weight: 500;
    }

    .arrow {
        font-size: 32rpx;
        color: #b2bec3;
        margin-left: 16rpx;
    }
}

/* 添加动态效果 */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20rpx); }
    to { opacity: 1; transform: translateY(0); }
}

.menu-item {
    animation: fadeIn 0.4s ease forwards;
    opacity: 0;
    
    @for $i from 1 through 10 {
        &:nth-child(#{$i}) {
            animation-delay: $i * 0.08s;
        }
    }
}
</style>